package com.angel.provider.sys.impl;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.angel.core.base.BaseProviderImpl;
import com.angel.core.util.MD5Util;
import com.angel.core.util.ResultParam;
import com.angel.core.util.StringUtils;
import com.angel.dao.health.SysUserMapper;
import com.angel.dao.health.SysUserRoleMapper;
import com.angel.health.SysUser;
import com.angel.health.SysUserExt;
import com.angel.health.SysUserRole;
import com.angel.health.SysUserRoleExample;
import com.angel.health.SysUserRoleExample.Criteria;
import com.angel.provider.sys.SysManagerUserProvider;

/**
 * 高亚林
 * 2016年9月20日下午1:30:06
 */

@Service
public class SysManagerUserProviderImpl extends BaseProviderImpl<SysUser>
		implements SysManagerUserProvider {

	@Autowired
	private SysUserMapper mapper;
	@Autowired
	private SysUserRoleMapper roleMapper;
	
	//批量删除
	public ResultParam delete(String ids) {
		ResultParam param = new ResultParam();
		try {
			SysUser user = new SysUser();
			if(StringUtils.isEmpty(ids)){
				param.setCode("0");
				param.setMsg("请选择要删除的对象");
				return param;
			}
			String[] idarr = ids.split("&");
			for (String id : idarr) {
				user.setId(Integer.parseInt(id));
				user.setDelFlag("0");
				mapper.updateByPrimaryKeySelective(user);
			}
			/*SysUser sysUser = mapper.selectByPrimaryKey(user.getId());
			param.setMsg(sysUser.getOfficeId()==null?"":sysUser.getOfficeId().toString());*/
			param.setCode("1");
		} catch (Exception e) {
			e.printStackTrace();
			param.setCode("0");
			param.setMsg("删除失败");
			return param;
		}
		return param;
	}

	//删除单个用户
	public ResultParam deleteById(SysUser user) {
		ResultParam param = new ResultParam();
		if(user == null){
			param.setCode("0");
			param.setMsg("请选择要删除的对象");
			return param;
		}
		user.setDelFlag("0");
		mapper.updateByPrimaryKeySelective(user);
		/*SysUser sysUser = mapper.selectByPrimaryKey(user.getId());
		param.setMsg(sysUser.getOfficeId()==null?"":sysUser.getOfficeId().toString());*/
		param.setCode("1");
		return param;
	}

	//根据id获取用户数据
	public List<SysUserExt> findUserById(SysUser user) {
		return mapper.findUserById(user);
	}

	//保存用户
	public int save(SysUser user, String role) {
		try {
			user.setCreateTime(new Date());
			user.setDelFlag("1");
			mapper.insert(user);
			if(!StringUtils.isEmpty(role)){
				Integer userId = user.getId();
				String[] roles = role.split(",");
				for (String roleId : roles) {
					SysUserRole userRole = new SysUserRole();
					userRole.setUserId(userId);
					userRole.setRoleId(Integer.parseInt(roleId));
					roleMapper.insert(userRole);
				}
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

	//修改用户
	public Map<String, String> update(SysUser user, String role) {
		Map<String, String> map = new HashMap<String, String>();
		if(user == null || user.getNo() == null || user.getNo() == ""){
			map.put("msg", "工号不能为空");
			map.put("code", "0");
			return map;
		}
		if(user.getName() == null || "".equals(user.getName())){
			map.put("msg", "姓名不能为空");
			map.put("code", "0");
			return map;
		}
		if(user.getLoginName() == null || "".equals(user.getLoginName())){
			map.put("msg", "用户名不能为空");
			map.put("code", "0");
			return map;
		}else if(user.getLoginName().length() < 6 || user.getLoginName().length() > 30){
			map.put("msg", "用户名字符长度要大于6位小于30位");
			map.put("code", "0");
			return map;
		}
		if(user.getPassword() == null || "".equals(user.getPassword())){
			map.put("msg", "密码不能为空");
			map.put("code", "0");
			return map;
		}
		if(!user.getPassword().equals(user.getConfirmPassword())){
			map.put("msg", "密码不一致");
			map.put("code", "0");
			return map;
		}
		user.setUpdateTime(new Date());
		if(!StringUtils.isEmpty(user.getPassword())){
			user.setPassword(MD5Util.getMD5(user.getPassword()));
		}
		mapper.updateByPrimaryKeySelective(user);
		SysUserRoleExample example = new SysUserRoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(user.getId());
		roleMapper.deleteByExample(example );
		if(!StringUtils.isEmpty(role)){
			String[] roles = role.split(",");
			for (String roleId : roles) {
				SysUserRole userRole = new SysUserRole();
				userRole.setUserId(user.getId());
				userRole.setRoleId(Integer.parseInt(roleId));
				roleMapper.insert(userRole);
			}
		}
		map.put("msg", "修改成功");
		map.put("code", "1");
		return map;
	}
	
	//查询所有用户
	public List<SysUser> findAllUser() {
		return mapper.findAllUsers();
	}

	@Override
	public SysUser findUserByUsernameAndPassword(String username, String password) {
		return mapper.findUserByUsernameAndPassword(username,password);
	}

	//修改图像
	public void updatePhoto(SysUser user) {
		mapper.updateByPrimaryKeySelective(user);
	}

	public List<Map<String, Object>> findAllUsers(SysUser user){
		return mapper.findAllUser(user);
	}
}
